package cn.tedu.bonfire.front.content.dao.persist.repository.impl;

import cn.tedu.bonfire.common.pojo.vo.PageData;
import cn.tedu.bonfire.common.util.PageInfoToPageDataConverter;
import cn.tedu.bonfire.front.content.dao.persist.mapper.ArticleMapper;
import cn.tedu.bonfire.front.content.dao.persist.repository.IArticleRepository;
import cn.tedu.bonfire.front.content.pojo.entity.Article;
import cn.tedu.bonfire.front.content.pojo.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;

@Slf4j
@Repository
public class ArticleRepositoryImpl implements IArticleRepository {

    @Autowired
    private ArticleMapper articleMapper;

    @Override
    public int insert(Article article) {
        return articleMapper.insert(article);
    }

    @Override
    public ArticleStandardVO getStandardById(Long id) {
        return articleMapper.getStandardById(id);
    }

    @Override
    public Long selectUserArticles(Long userId) {
        return articleMapper.selectCount(userId);
    }

    @Override
    public Long selectUserLikes(Long userId) {
        return articleMapper.selectSumLike(userId);
    }


    @Override
    public Long selectUserViews(Long userId) {
        return articleMapper.selectView(userId);
    }

    @Override
    public Long selectUserCollect(Long userId) {
        return articleMapper.selectCountcollect(userId);

    }
    @Override
    public PageData<ArticleListItemVO> listByCategory(Long categoryId, Integer isRecommend) {
        log.debug("开始执行【根据类别查询文章列表】的数据访问，文章类别,是否推荐", categoryId, isRecommend);
        List<ArticleListItemVO> list = articleMapper.listByCategory(categoryId);
        PageInfo<ArticleListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
    @Override
    public int updateViewById(Long id) {
        log.debug("开始执行【浏览量】的数据访问，文章类别：{}，页码：{}，每页记录数：{}",id);
        return articleMapper.updateViewCountById(id);
    }
    @Override
    public int deleteById(Long id) {
        log.debug("开始执行【根据ID删除文章】的数据访问，参数：{}", id);
        return articleMapper.deleteById(id);
    }

    @Override
    public PageData<ArticleListItemVO> listByRecommend() {
        log.debug("开始执行【查询推荐的文章列表】的数据访问");
        List<ArticleListItemVO> list = articleMapper.listByRecommend();
        PageInfo<ArticleListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }




    /**
     * 文章管理页面--根据id修改笔记审核状态
     *
     * @param  article 要修改的实体类
     * @return 受影响的行数
     */
    @Override
    public int updateArticle(Article article) {
        return articleMapper.updateById(article);
    }

    @Override
    public PageData<UserDraftListVO> selectdraft(Long userId,Integer pageNum,Integer pageSize) {
        log.debug("开始执行【根据用户id查询草稿列表】的数据访问，用户id：{}，页码：{}，每页记录数：{}", userId, pageNum, pageSize);
        PageHelper.startPage(pageNum,pageSize);
        List<UserDraftListVO> list = articleMapper.selectdraft(userId);
        PageInfo<UserDraftListVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }
    /**
     * 根据用户id查询自己的文章(审核通过的)
     *
     */
    @Override
    public List<UserCollectListVO> selectMyAuditArticle(Long userId) {

            return articleMapper.selectMyAuditArticle(userId);
    }

    @Override
    public UserDraftListVO selectDraftDetail(Long authorId, Long articleId) {
        return articleMapper.selectDraftDetail(authorId,articleId);
    }

    @Override
    public PageData<ArticleListItemVO> listByCategory(Long categoryId, Integer pageNum, Integer pageSize) {
        log.debug("开始执行【根据类别查询文章列表】的数据访问，文章类别：{}，页码：{}，每页记录数：{}", categoryId, pageNum, pageSize);
        PageHelper.startPage(pageNum, pageSize);
        List<ArticleListItemVO> list = articleMapper.listByCategory(categoryId);
        PageInfo<ArticleListItemVO> pageInfo = new PageInfo<>(list);
        return PageInfoToPageDataConverter.convert(pageInfo);
    }



    @Override
    public int setCommentCount(Long articleId, Integer commentCount) {
        log.debug("开始执行【设置文章的评论数】的数据访问，参数：{}", articleId);
        Article article = new Article();
        article.setId(articleId);
        article.setCommentCount(commentCount);
        return articleMapper.updateById(article);
    }

    @Override
    public ArticleAuthorVO getAuthorByArticleId(Long id) {
        return articleMapper.getAuthorByArticleId(id);
    }
    /**
     * 根据用户id查询自己的文章
     *
     */
    @Override
    public List<UserCollectListVO> selectMyArticle(Long userId) {
        return articleMapper.selectMyArticle(userId);
    }

    /**
     * 根据点赞查询文章*/
    @Override
    public List<UserCollectListVO> selectLike(Long userId) {
        return articleMapper.selectLike(userId);
    }

    /**
     * 根据点赞查询文章进行排序*/
    @Override
    public List<UserCollectListVO> getIsrecommend() {
        return articleMapper.getIsrecommend();
    }

    /**
     * 获取收藏文章所需信息
     * @param authorId
     * @return
     */
    @Override
    public List<UserCollectListVO> selectCollect(Long authorId) {
        return articleMapper.selectCollect(authorId);
    }
}
